package com.example.customview.expandtextview

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.example.customview.R

/**
 * 自定义展开文本
 * yf
 */
class ExpandLayout2 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defaultInt: Int = 0
) : ConstraintLayout(context, attributeSet, defaultInt) {
    private var originText : String? = null // 原始内容文本
    private var mMaxLines = 2 // TextView最大行数
    private var expandTextView:ExpandTextView2
    private var expandButton :TextView
    private var topic:List<String>?=null
    private var isExpand = false

    /**
      *<!--话题颜色-->
     */
    private var mTopicColor =Color.BLACK
    /**
     *<!--默认文本颜色-->
     */
    private var mDefaultColor =Color.BLACK
    /**
     *<!--展开按钮颜色-->
     */
    private var mExpandBtnColor =Color.BLACK
    /**
     *<!--展开按钮是否加粗-->
     */
    private var mExpandBtnBold =true
    /**
     *<!--话题是否加粗-->
     */
    private var mTopicBold =true
    /**
     *<!--文本最大高度-->
     */
    private var mTextMaxHeight =0
    /**
     *<!--文本行数-->
     */
    private var mTextMaxLines =0
    /**
     *<!--展开文本字体大小-->
     */
    private var mExpandBtnTextSize =16f
    /**
     *<!---默认文本字体大小-->
     */
    private var mDefaultTextSize =16f
    /**
     *<!---默认文本字体大小-->
     */
    private var mExpandBtnMarginTop =0

    init {
        val attributes = context.obtainStyledAttributes(attributeSet, R.styleable.ExpandLayout)
        mDefaultColor = attributes.getColor(R.styleable.ExpandLayout_defaultColor,Color.BLACK)
        mTopicColor = attributes.getColor(R.styleable.ExpandLayout_topicColor,getColor(R.color.color_0091ff))
        mExpandBtnColor = attributes.getColor(R.styleable.ExpandLayout_expandBtnColor,getColor(R.color.color_0091ff))
        mExpandBtnBold = attributes.getBoolean(R.styleable.ExpandLayout_expandBtnBold,false)
        mTopicBold = attributes.getBoolean(R.styleable.ExpandLayout_topicBold,false)
        mTextMaxHeight = attributes.getDimensionPixelSize(R.styleable.ExpandLayout_textMaxHeight,mTextMaxHeight)
        mTextMaxLines = attributes.getInt(R.styleable.ExpandLayout_textMaxLines,mTextMaxLines)
        mExpandBtnTextSize = attributes.getDimension(R.styleable.ExpandLayout_expandBtnTextSize,mExpandBtnTextSize)
        mDefaultTextSize = attributes.getDimension(R.styleable.ExpandLayout_defaultTextSize,mDefaultTextSize)
        mExpandBtnMarginTop = attributes.getDimensionPixelSize(R.styleable.ExpandLayout_expandBtnMarginTop,mExpandBtnMarginTop)

        attributes.recycle()


        expandTextView = ExpandTextView2(context).apply {
              layoutParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT).apply {
              startToStart =LayoutParams.PARENT_ID
              topToTop = LayoutParams.PARENT_ID

          }
        }
        expandTextView.id = View.generateViewId()
        expandTextView.setTextColor(mDefaultColor)
        expandTextView.maxLines =mTextMaxLines
        expandTextView.textSize = mDefaultTextSize

        //展开按钮
        expandButton = TextView(context)
        expandButton.layoutParams = ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT).apply {
            endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
            topToBottom = expandTextView.id
            topMargin = mExpandBtnMarginTop

        }
        expandButton.visibility = View.GONE
        expandButton.typeface = if (mExpandBtnBold)Typeface.DEFAULT_BOLD else Typeface.DEFAULT
        expandButton.text = "收起"
        expandButton.textSize = mExpandBtnTextSize
        expandButton.setTextColor(mExpandBtnColor)

        addView(expandTextView)
        addView(expandButton)
        initListener()
    }

    private fun initListener() {
         expandTextView.setExpandListener(object :ExpandTextView2.OnExpandListener{
             override fun onExpand(view: ExpandTextView2?) {
                 expandButton.visibility = View.VISIBLE
             }

         })
        expandButton.setOnClickListener {
            expandButton.visibility = View.GONE
            expandTextView.maxLines =mMaxLines
            expandTextView.setCloseText(originText)
        }
    }

    fun  setTextContent(content:String,topic:List<String>){
        this.originText= content
        expandTextView.setAtIds(topic)
        expandTextView.setCloseText(content)
    }



    fun getColor(resId: Int): Int {
        return ContextCompat.getColor(context,resId)
    }
}